package com.example.xln_sideslipmenu;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import java.text.DecimalFormat;
import androidx.annotation.Nullable;

/**
 * author by LiuGuo
 * on 2021/4/9
 * 自定义组件：圆形进度条
 */
public class CircleProgressView extends View {
    private Paint mPaint;
    private Paint mPaint1;
    private Paint mPaint2;
    private RotateAnimation anim;
    private float mDegrees;
    private int widthsize;
    private int heightsize;
    private int initialColor;
    private int finishColor;
    private Paint mPaint3;
    boolean startTag = false;
    DecimalFormat decimalFormat = new DecimalFormat("00");
    private int paddingLeft=10;
    private int paddingTop=10;
    private int paddingRight=10;
    private int paddingBottom=10;

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.crilProgress);
        initialColor = ta.getColor(R.styleable.crilProgress_initialColor, Color.parseColor("#F4F1F4"));
        finishColor = ta.getColor(R.styleable.crilProgress_finishColor, Color.parseColor("#FED942"));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setBackgroundColor(Color.parseColor("#00000000"));
        init();
        widthsize = MeasureSpec.getSize(widthMeasureSpec);
        heightsize = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(widthsize, heightsize);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
/*        paddingLeft = getPaddingLeft();  //根据实际paddin
        paddingTop = getPaddingTop();
        paddingRight = getPaddingRight();
        paddingBottom = getPaddingBottom();*/
        RectF rectF1 = new RectF(widthsize / 12.5f+paddingLeft, heightsize / 12.5f+paddingTop, widthsize / 12.5f + (widthsize / 1.2f)-paddingRight, heightsize / 12.5f + (heightsize / 1.2f)-paddingBottom);
        if (mDegrees < 360) {
            if (!startTag) {
                canvas.drawArc(rectF1, 90, 360, false, mPaint);
                canvas.drawLine(widthsize / 4.1f, widthsize / 1.7f, widthsize / 2.03f, widthsize / 1.335f, mPaint);
                canvas.drawLine(widthsize / 2.1f, widthsize / 1.38f, widthsize / 1.38f, widthsize / 2.77f, mPaint);
            } else {
                canvas.drawArc(rectF1, 90, mDegrees, false, mPaint1);
                canvas.drawText(decimalFormat.format(mDegrees / 360 * 100) + "%", heightsize / 3.9f+paddingLeft*0.4f, heightsize / 1.65f-paddingTop*0.1f, mPaint3);
            }
            invalidate();
        } else {
            canvas.drawCircle(widthsize / 2, heightsize / 2, heightsize / 2.7f, mPaint2);
            canvas.drawLine(widthsize / 4.1f, widthsize / 1.7f, widthsize / 2.03f, widthsize / 1.335f, mPaint);
            canvas.drawLine(widthsize / 2.1f, widthsize / 1.38f, widthsize / 1.38f, widthsize / 2.77f, mPaint);
        }
    }



    /**
     *  0未下载  1准备下载（设置进度前）  2下载完成  3下载错误
     * @param type
     */
    public void setState(int type) {
        switch (type) {
            case 0:
                startTag = false;
                mDegrees = 0;
                invalidate();
                break;

            case 1:
                startTag = true;
                mDegrees = 0;
                invalidate();
                break;

            case 2:
                startTag = true;
                mDegrees = 361;
                invalidate();
                break;

            case 3:
                startTag = true;
                mPaint1.setColor(Color.parseColor("#ff0000"));
                mPaint3.setColor(Color.parseColor("#ff0000"));
                invalidate();
                break;
        }
    }



    /**
     * 设置进度
     * @param progress
     */
    public void setProgress(float progress) {
        if (startTag)
            this.mDegrees = progress / 100 * 360;
        invalidate();
    }



    /**
     * 初始化
     */
    public void init() {
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        BlurMaskFilter blu=new BlurMaskFilter(1,BlurMaskFilter.Blur.SOLID);
        mPaint = new Paint();

        mPaint.setColor(initialColor);
        mPaint.setStrokeWidth(widthsize / 17.85f);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.STROKE); //绘制空心圆
        mPaint.setFilterBitmap(true);
//        mPaint.setMaskFilter(blu);
        mPaint.setShadowLayer(7,7,7,Color.parseColor("#55000000"));

        mPaint1 = new Paint();
        mPaint1.setColor(finishColor);
        mPaint1.setStrokeWidth(widthsize / 17.85f);
        mPaint1.setAntiAlias(true);
        mPaint1.setDither(true);
        mPaint1.setStyle(Paint.Style.STROKE); //绘制空心圆
        mPaint1.setFilterBitmap(true);
//        mPaint1.setMaskFilter(blu);
        mPaint1.setShadowLayer(7,9,9,Color.parseColor("#55000000"));

        mPaint2 = new Paint();
        mPaint2.setColor(finishColor);
        mPaint2.setStrokeWidth(widthsize / 17.85f);
        mPaint2.setAntiAlias(true);
        mPaint2.setDither(false);
        mPaint2.setStyle(Paint.Style.FILL_AND_STROKE); //绘制空心圆
        mPaint2.setFilterBitmap(true);
//        mPaint2.setMaskFilter(blu);
        mPaint2.setShadowLayer(7,9,9,Color.parseColor("#55000000"));

        mPaint3 = new Paint();
        mPaint3.setColor(finishColor);
        mPaint3.setTextSize(widthsize / 3.85f);
        mPaint3.setFakeBoldText(true);
        mPaint3.setFilterBitmap(true);
        setState(0);
    }


    private void createAnimation(Canvas canvas) {
        anim = new RotateAnimation(0, 360, getWidth() / 2, getHeight() / 2);
        anim.setRepeatMode(Animation.RESTART);
        anim.setFillAfter(true);
        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatCount(100);
        anim.setDuration(3000);
        startAnimation(anim);
    }


}
